/**
 * Created with  SublimeText
 * @Author:      Lyle
 * @DateTime:    2017-05-27 10:49:10
 * @Description: 权限领域相关业务逻辑公共
 */

import globalMixin from '../common/globalMixin'

var permissionPartMixin = {

    mixins: [globalMixin],

    methods: {

        syncLogin: function() {
            if (Unit.getCookie('session_id')) {
                this.$store.dispatch('user_syncLogin', this);
            }
        },

        syncLoginCallback: function(data) {
            console.log('syncLoginCallback', data.errcode);
            if (typeof(data.errcode) == 'undefined') {
                this.permissionPartMixin_LoginSuccessful(data);
            } else {
                this.$store.dispatch('user_loginOut', this);
            }
        },

        permissionPartMixin_LoginSuccessful: function(data, callback) {
            if (Env == 'dev') {
                Unit.setCookie('client_key', new Date().getTime(), { path: '/', domain: Unit.getRootDomain() });
                Unit.setCookie('session_id', data.session_id, { path: '/', domain: Unit.getRootDomain() });
            }

            //get status
            this.$store.dispatch('requestSiteNumStateData', { flush_cache: 1 });

            // Unit.setSessionStorage("ms_myself", JSON.stringify(data));
            Unit.setSessionStorage('ms_agree', data.agree_pp_sa);
            if (callback) {
                callback();
            } else {
                this.permissionPartMixin_LoginSuccessfulAfter(data);
            }
        },

        permissionPartMixin_LoginSuccessfulAfter: function(data) {
            if (parseInt(data.agree_pp_sa) == 1) {
                if (!this.permissionPartMixin_checkCompleteProfile()) {
                    this.$router.push({
                        name: 'completeProfile'
                    });
                } else {
                    this.$router.push({ 'name': 'browse' });
                }
            } else {
                this.$router.push({ 'name': 'agreeServicePrivacy' });
            }
        },

        permissionPartMixin_checkCompleteProfile() {
            var bool = true,
                userInfo = Unit.getObjSessionStorage('ms_myself');
            if (parseInt(userInfo.status.isGuest) && (!userInfo.about.headline || !userInfo.about.about_me || !userInfo.about.about_my_match)) {
                bool = false;
            }
            return bool;
        },

        permissionPartMixin_BrowserClear: function() {
            Unit.delCookie('client_key', { path: '/', expires: -1, domain: Unit.getRootDomain(Domain) });
            Unit.delCookie('session_id', { path: '/', expires: -1, domain: Unit.getRootDomain(Domain) });
            Unit.delCookie('password', { path: '/', expires: -1, domain: Unit.getRootDomain(Domain) });
            sessionStorage.clear();
            //API&store need use,so not use this
            window.location.reload();
            //VueInstance._router.push({ 'name': 'login' });
        },

        permissionPartMixin_SelectValue: function(mid, value) {
            let select = JSON.parse(Unit.getLocalStorage(mid));
            for (let i in select) {
                if (select[i].value == value) {
                    return select[i].label;
                }
            }
            return this.language.unify_askMe;
        },

        permissionPartMixin_MultiSelectStr: function(mid, val, spe) {
            let arr = [],
                str = '',
                select = JSON.parse(Unit.getLocalStorage(mid));
            // spe = spe ? spe '; ';
            if (select.length > 0) {
                for (let i in select) {
                    if (parseInt(parseInt(val) & parseInt(select[i].value)) > 0) {
                        arr.push(select[i].label);
                    }
                }
                str = arr.join('; ');
            }
            return str;
        }
    }

}

export default permissionPartMixin;
